package com.v1.video.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.domain.LoginInfo;
import com.v1.video.domain.RewardVideoConfig;
import com.v1.video.domain.RewardVideoConfig.RewardVideo;
import com.v1.video.domain.ScenarioVideoInfo;
import com.v1.video.engine.NetEngine;
import com.v1.video.util.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单支付成功页
 * 
 * @author wangfang
 * 
 */
public class RewardSuccessActivity extends BaseActivity implements
		OnClickListener {

	private TextView mTvTitle, mTvName, mTvMoney;
	private RecommendVideoAdapter mAdapter;
	private GridView mGridView;
	private String mVideoId;
	private View mView;
	private ProgressDialog pd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_rewardsuccess);
	}

	/**
	 * 初始化界面
	 */
	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		mView = this.findViewById(R.id.lay_result);
		mTvTitle = (TextView) this.findViewById(R.id.tv_title);
		mTvName = (TextView) this.findViewById(R.id.tv_name);
		mTvMoney = (TextView) this.findViewById(R.id.tv_money);
		mGridView = (GridView) this.findViewById(R.id.gv_gridview);
		mView.setVisibility(View.GONE);
	}

	/**
	 * 初始化数据
	 */
	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		Intent intent = getIntent();
		String mName = intent.getStringExtra("name");
		String mMoney = intent.getStringExtra("money");
		mVideoId = intent.getStringExtra("videoId");
		mTvTitle.setText("打赏成功");
		if (!TextUtils.isEmpty(mName)) {
			mTvName.setText("视频名称：" + mName);
		} else {
			mTvName.setText("视频名称：");
		}
		if (!TextUtils.isEmpty(mMoney)) {
			mTvMoney.setText(mMoney + "元");
		} else {
			mTvMoney.setText("0元");
		}
		mAdapter = new RecommendVideoAdapter(this);
		mGridView.setAdapter(mAdapter);
		new RewardVideoAsyncTask().execute();
	}

	/**
	 * 设置事件
	 */
	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		this.findViewById(R.id.btn_video).setOnClickListener(this);
		this.findViewById(R.id.btn_record).setOnClickListener(this);
	}

	/**
	 * 事件处理
	 */
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_video:
			if (!TextUtils.isEmpty(mVideoId)) {
				ScenarioVideoInfo video = new ScenarioVideoInfo(mVideoId);
				if (video != null) {
					Intent intent = new Intent();
					intent.setClass(this, PaikeVideoDetailActivity2.class);
					intent.putExtra("videoInfo", video);
					intent.putExtra("Flag", "1");
					startActivity(intent);
				}
			}
			break;
		case R.id.btn_record:
			Intent intent = new Intent();
			intent.setClass(this, MyShouYiActivity.class);
			intent.putExtra("rewardStyle", 1);
			startActivity(intent);
			break;
		default:
			break;
		}
	}

	/**
	 * 物理键处理
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent intent = new Intent();
			intent.setClass(this, MainPageNewActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
			startActivity(intent);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * 推荐视频适配器列表
	 * 
	 * @author wangf
	 * 
	 */
	private class RecommendVideoAdapter extends BaseAdapter {

		private LayoutInflater mLayoutInflater;
		private List<RewardVideo> mLstData;
		private Context mContext;
		private int mHeight;

		public RecommendVideoAdapter(Context context) {
			mLayoutInflater = LayoutInflater.from(context);
			mHeight = Utils.computeImageHeight(context);
			mLstData = new ArrayList<RewardVideo>();
			mContext = context;
		}
		
		public List<RewardVideo> getmLstData() {
			return mLstData;
		}

		public void setmLstData(List<RewardVideo> mLstData) {
			this.mLstData = mLstData;
			this.notifyDataSetChanged();
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mLstData.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mLstData.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			Holder holder = null;
			if (convertView == null) {
				convertView = mLayoutInflater.inflate(
						R.layout.grid_waterfall_item, null);
				holder = new Holder();
				holder.layItem = (LinearLayout) convertView
						.findViewById(R.id.lay_item);
				holder.image = (ImageView) convertView
						.findViewById(R.id.iv_image);
				holder.title = (TextView) convertView
						.findViewById(R.id.tv_title);
				holder.subtitle = (TextView) convertView
						.findViewById(R.id.tv_subtitle);
				convertView.setTag(holder);
			} else {
				holder = (Holder) convertView.getTag();
			}
			final RewardVideo reward = mLstData.get(position);
			if (!TextUtils.isEmpty(reward.getRecommendTitle())) {
				holder.title.setText(reward.getRecommendTitle());
			} else if (!TextUtils.isEmpty(reward.getTitle())) {
				holder.title.setText(reward.getTitle());
			} else {
				holder.title.setText("");
			}
			if (!TextUtils.isEmpty(reward.getRecommendDetail())) {
				holder.subtitle.setText(reward.getRecommendDetail());
			} else if (reward.getUserInfo() != null
					&& (!TextUtils.isEmpty(reward.getUserInfo().getNickname()) || !TextUtils
							.isEmpty(reward.getUserInfo().getUserName()))) {
				if (!TextUtils.isEmpty(reward.getUserInfo().getNickname())) {
					holder.subtitle.setText(reward.getUserInfo().getNickname());
				} else if (!TextUtils.isEmpty(reward.getUserInfo().getUserName())) {
					holder.subtitle.setText(reward.getUserInfo().getUserName());
				}
				holder.subtitle.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if (reward != null
								&& reward.getUserInfo() != null
								&& !TextUtils.isEmpty(reward.getUserInfo()
										.getUserId())) {
							String userId = reward.getUserInfo().getUserId();
							Intent intent = new Intent();
							if (LoginInfo.getInstance().isLogin()) {
								if (!TextUtils.isEmpty(userId)
										&& userId.equals(LoginInfo
												.getInstance().getUserId())) {
//									intent.setClass(mContext,
//											PersonalHomeActivity.class);
//									mContext.startActivity(intent);
								} else {
									intent.setClass(mContext,
											OtherPersonHomeActivity.class);
									intent.putExtra("userId", userId);
									mContext.startActivity(intent);
								}
							} else {
								intent.setClass(mContext,
										OtherPersonHomeActivity.class);
								intent.putExtra("userId", userId);
								mContext.startActivity(intent);
							}
						}
					}
				});
			} else {
				holder.subtitle.setText("");
			}
			android.view.ViewGroup.LayoutParams Params = holder.image
					.getLayoutParams();
			Params.width = LayoutParams.MATCH_PARENT; // 设置图片的宽度
			Params.height = mHeight;// 设置图片的高度
			holder.image.setLayoutParams(Params);
			if (TextUtils.isEmpty(reward.getImgUrl())) {
				holder.image.setImageResource(R.drawable.video9_16_loading);
			} else {
				ImageLoader.getInstance().displayImage(reward.getImgUrl(),
						holder.image, Constant.VIDEO_9_16_OPTION, null);
			}
			holder.layItem.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if (reward != null
							&& !TextUtils.isEmpty(reward.getVideoId())) {
						Intent intent = new Intent();
						ScenarioVideoInfo video = new ScenarioVideoInfo(reward
								.getVideoId() + "");
						if (video != null) {
							intent.setClass(mContext,
									PaikeVideoDetailActivity2.class);
							intent.putExtra("videoInfo", video);
							intent.putExtra("Flag", "1");
							mContext.startActivity(intent);
						}
					}
				}
			});
			return convertView;
		}

		private class Holder {
			public LinearLayout layItem;
			public ImageView image;
			public TextView title;
			public TextView subtitle;
		}
	}
	
	
	/**
	 * 获取支付成功视频列表
	 * 
	 * @author wangf
	 * 
	 */
	private class RewardVideoAsyncTask extends
			AsyncTask<Void, Void, RewardVideoConfig> {
		
		private String errorMsg;
		
		@Override
		protected void onPreExecute() {
			pd = Utils.getProgressDialog(RewardSuccessActivity.this,this);
			pd.show();
		}

		@Override
		protected RewardVideoConfig doInBackground(Void... params) {

			try {
				return new NetEngine().getPaySuccessVideoList();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				errorMsg = e.getMessage();
			}
			return null;
		}

		@Override
		protected void onPostExecute(RewardVideoConfig result) {
			if (pd != null && pd.isShowing()) {
				pd.cancel();
			}
			pd = null;
			if (result != null && result.getObj() != null && mAdapter!=null) {
				mAdapter.setmLstData(result.getObj());
			} else {
				if (!TextUtils.isEmpty(errorMsg)) {
					Toast.makeText(RewardSuccessActivity.this, errorMsg,
							Toast.LENGTH_SHORT).show();
				}
			}
		}
	}
}
